package geektime.spring.springbucks.service;

import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.exact;

import geektime.spring.springbucks.mondel.Coffee;
import geektime.spring.springbucks.mondel.CoffeeCache;
import geektime.spring.springbucks.repository.CoffeeCacheRepository;
import geektime.spring.springbucks.repository.CoffeeRepository;
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CoffeeService {

  @Autowired
  private CoffeeRepository coffeeRepository;
  @Autowired
  private CoffeeCacheRepository coffeeCacheRepository;

  public List<Coffee> findAllCoffee() {
    return coffeeRepository.findAll();
  }

  public Optional<Coffee> findSimpleCoffeeFromCache(String name) {
    Optional<CoffeeCache> cached = coffeeCacheRepository.findOneByName(name);
    if (cached.isPresent()) {
      CoffeeCache coffeeCache = cached.get();
      Coffee coffee = Coffee.builder().name(coffeeCache.getName()).price(coffeeCache.getPrice())
          .build();
      log.info("Coffee {} found in cache.", coffeeCache);
      return Optional.of(coffee);
    } else {
      Optional<Coffee> raw = findOneCoffee(name);
      raw.ifPresent(c -> {
        CoffeeCache cache = CoffeeCache.builder().id(c.getId()).name(c.getName())
            .price(c.getPrice()).build();
        log.info("Save Coffee {} to cache.", cache);
        coffeeCacheRepository.save(cache);
      });
      return raw;
    }

  }

  public Optional<Coffee> findOneCoffee(String name) {
    ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name", exact().ignoreCase());
    Optional<Coffee> coffee = coffeeRepository
        .findOne(Example.of(Coffee.builder().name(name).build(), matcher));
    log.info("Coffee Found:{}", coffee);
    return coffee;

  }
}
